package com.ruoyi.electricity.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.electricity.domain.ElectricityEquipType;
import com.ruoyi.electricity.service.ElectricityEquipTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ljw
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/electricity/equipType")
public class ElectricityEquipTypeController extends BaseController {

    @Autowired
    private ElectricityEquipTypeService electricityEquipTypeService;

    /**
     * 添加、修改或删除 equip 和 equipType 的关联
     */
    @PutMapping("associatedEquipAndEquipType")
    public AjaxResult associatedEquipAndEquipType(String equipCode, Integer equipTypeId)
    {
        if (equipCode == null) {
            return AjaxResult.error("设备编号不能为空");
        }
        electricityEquipTypeService.deleteEquipAndEquipTypeAssociation(equipCode);
        if (equipTypeId != null) {
            electricityEquipTypeService.updateEquipAndEquipTypeAssociation(equipCode, equipTypeId);
        }
         return AjaxResult.success();
    }

    /**
     * 查询设备类型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ElectricityEquipType electricityEquipType)
    {
        startPage();
        List<ElectricityEquipType> list = electricityEquipTypeService.selectElectricityEquipTypeList(electricityEquipType);
        return getDataTable(list);
    }

    /**
     * 获取设备类型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(electricityEquipTypeService.selectElectricityEquipTypeById(id));
    }
    /**
     * 新增设备类型
     */
    @PostMapping
    public AjaxResult add(@RequestBody ElectricityEquipType electricityEquipType)
    {
        return toAjax(electricityEquipTypeService.insertElectricityEquipType(electricityEquipType));
    }

    /**
     * 修改设备类型
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ElectricityEquipType electricityEquipType)
    {
        return toAjax(electricityEquipTypeService.updateElectricityEquipType(electricityEquipType));
    }
    /**
     * 删除设备类型
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return AjaxResult.success(electricityEquipTypeService.deleteElectricityEquipTypeByIds(ids));
    }

}
